package com.wlw.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wlw.entity.QA;
import com.wlw.entity.QAreply;
import com.wlw.entity.User;
import com.wlw.service.QAService;
import com.wlw.service.QAreplyService;

import edu.xjsoft.base.orm.support.Page;
import edu.xjsoft.base.orm.support.PropertyFilter;

@Controller
@RequestMapping("/qaManage/*")
public class QAAction {

	@Autowired
	private QAService qaService;
	@Autowired
	private QAreplyService qAreplyService;

	/**
	 * 前台问答模块：
	 * @param pageNo
	 * @param model
	 * @return
	 */
	@RequestMapping("quest")
	public String quest(HttpServletRequest req,Model model){
		//最新问题：
		List<QA> newQas = qaService.qAlist();
		model.addAttribute("newQas", newQas);
		//全部问题：
		int pageNo = Integer.parseInt(req.getParameter("pageNo"));
		System.out.println(pageNo);
		Page<QA> page = new Page<QA>(5);
		page.setPageNo(pageNo);
		qaService.loadAll(page);
		model.addAttribute("page", page);
		return "foreground/quest";
	}
	@RequestMapping("questDetail")
	public String questDetail(HttpServletRequest req,Model model){
		int id = Integer.parseInt(req.getParameter("id"));
		PropertyFilter filters = new PropertyFilter("id", id);
		List<QA> qas = qaService.search(filters);
		model.addAttribute("qas", qas);
		return "foreground/ListQaReply";
	}
	
	@RequestMapping("addQuestion")
	public String addQuestion(HttpServletRequest req,Model model){
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		System.out.println("标题："+title+",内容："+content);
		Date time = new Date();
		User user = (User) req.getSession().getAttribute("user");
		QA qa = new QA();
		if (user!=null&&title!=null) {
			qa.setTitle(title);
			qa.setContent(content);
			qa.setTime(time.toLocaleString());
			qa.setUser(user);
			qaService.saveOrUpdate(qa);
		}
		PropertyFilter filters = new PropertyFilter("title", title);
		List<QA> qas = qaService.search(filters);
		model.addAttribute("qas", qas);
		System.out.println(qas.get(0).getTitle());
		return "foreground/ListQaReply";
	}
	@RequestMapping("addReply")
	public String addReply(HttpServletRequest req,Model model){
		String content = req.getParameter("content");
		int id = Integer.parseInt(req.getParameter("id"));
		User user = (User) req.getSession().getAttribute("user");
		PropertyFilter filters = new PropertyFilter("id", id);
		List<QA> qas = qaService.search(filters);
		Date time = new Date();
		QAreply reply = new QAreply();
		if (user!=null&&content!=null) {
			reply.setUser(user);
			reply.setTime(time.toLocaleString());
			reply.setContent(content);
			reply.setQa(qas.get(0));
			qAreplyService.saveOrUpdate(reply);
		}
		model.addAttribute("qas", qas);
		return "foreground/ListQaReply";
	}
	
///以下是后台代码：
//////////////////////////////////////////////////////////	
	@RequestMapping("{pageNo}")
	public String list(@PathVariable int pageNo,Model model){
		Page<QA> page = new Page<QA>(5);
		page.setPageNo(pageNo);
		qaService.loadAll(page);
		model.addAttribute("page", page);
		return "background/qaManage";
	}
	@RequestMapping("delQa")
	public String delQa(HttpServletRequest req){
		int id = Integer.parseInt(req.getParameter("id"));
		qaService.remove(id);
		return "background/successDel";
	}
	@RequestMapping("multiDel")
	public String multiDel(HttpServletRequest req){
		String name[] = req.getParameterValues("checkbox");
		for (String i : name) {
			int id = Integer.parseInt(i);
			qaService.remove(id);
		}
		return "/background/successDel";
	}
}
